package fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Scroller;
import android.widget.TextView;

import com.example.zx.theline.R;

import GsonBean.CheckInfoBean;
import app.MyApplication;
import dialog.CheckPlanDialog;

import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.List;

import GsonBean.CheckPlanBean;
import base.BaseFragment;
import dialog.EditorGuardianDialog;
import network.NetWork;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import tools.MyToast;


public class CheckFragment extends BaseFragment {

    ListView mListView;
    CheckListAdapter mCheckListAdapter;
    CheckPlanBean mCheckPlanBean;
    List<CheckPlanBean.RowsBean> mSoureList;
    int CheckOffset = 1;
    int CheckLimit = 20;

    EditorGuardianDialog mEditorGuardianDialog;

    public CheckFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_check, container, false);
        mListView = (ListView) view.findViewById(R.id.listview);

        InitData();
        InitListen();

        return view;
    }


    @Override
    protected void InitData() {
        if (mSoureList == null) {
            getCheckListData(CheckOffset, CheckLimit);
        }
    }

    @Override
    protected void InitListen() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                getCheckInfoData(mSoureList.get(position).getBh());

            }
        });
    }

    //获取施工监护listview的数据 并更新

    public void getCheckListData(int offset, int limit) {
        RequestParams params = new RequestParams();
        params.add("offset", String.valueOf(offset));
        params.add("limit", String.valueOf(limit));
        NetWorkApi.getCheckListData(params, new ObjectHttpResponseHandler(getContext(), CheckPlanBean.class) {
            @Override
            public void Success(int i, org.apache.http.Header[] headers, Object object) {
                mCheckPlanBean = (CheckPlanBean) object;
                if(mCheckPlanBean.getMsg().equals("success")&&mCheckPlanBean.getTotal()==0){
                    MyToast.showToast("数据获取成功，当前没有数据");
                }
                else {
                    mSoureList = mCheckPlanBean.getRows();
                    if (mCheckListAdapter == null) {
                        mCheckListAdapter = new CheckListAdapter();
                    }
                    mListView.setAdapter(mCheckListAdapter);
                    mCheckListAdapter.notifyDataSetChanged();
                }
            }

        });

    }

    //点击listview以后根据点击的item获取数据并显示在dialog
    public void getCheckInfoData(String bh) {
        RequestParams params = new RequestParams();
        params.add("bh", "LR00162");
        NetWorkApi.getCheckInfoData(params, new ObjectHttpResponseHandler(getContext(), CheckInfoBean.class) {
            @Override
            public void Success(int i, Header[] headers, Object object) {
                CheckInfoBean mCheckInfoBean=(CheckInfoBean) object;
                showCheckPlanDialog(mCheckInfoBean);

            }

        });

    }

    public void showCheckPlanDialog(CheckInfoBean mCheckInfoBean){
        CheckPlanDialog mCheckPlanDialog=new CheckPlanDialog(getContext(),mCheckInfoBean);
        mCheckPlanDialog.show();
    }


    public class CheckListAdapter extends BaseAdapter {



        @Override
        public int getCount() {
            return mSoureList.size();
        }

        @Override
        public Object getItem(int position) {
            return mSoureList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder mHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.checklist_adapter, null);
                mHolder = new ViewHolder();
                mHolder.jihuahao = (TextView) convertView.findViewById(R.id.jihuahao);
                mHolder.shigongriqi = (TextView) convertView.findViewById(R.id.shigongriqi);
                mHolder.shigongleixing = (TextView) convertView.findViewById(R.id.shigongleixing);
                mHolder.shigongdidian = (TextView) convertView.findViewById(R.id.shigongdidian);
                mHolder.shigongxiangmu = (TextView) convertView.findViewById(R.id.shigongxiangmu);
                mHolder.jianhurenyuan = (TextView) convertView.findViewById(R.id.jianhurenyuan);
                mHolder.edit = (ImageView) convertView.findViewById(R.id.edit);
                mHolder.editlayout = (LinearLayout) convertView.findViewById(R.id.editlayout);
                mHolder.bjjhr = (TextView) convertView.findViewById(R.id.bjjhr);
                mHolder.bcfxd = (TextView) convertView.findViewById(R.id.bcfxd);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ViewHolder) convertView.getTag();
            }
            mHolder.jihuahao.setText("" + mSoureList.get(position).getBh() + "");
            mHolder.shigongriqi.setText("" + mSoureList.get(position).getSgrq() + "");
            mHolder.shigongleixing.setText("" + mSoureList.get(position).getLb() + "");
            mHolder.shigongdidian.setText("" + mSoureList.get(position).getSgdd() + "");
            mHolder.shigongxiangmu.setText("" + mSoureList.get(position).getXmmc() + "");
            mHolder.jianhurenyuan.setText("" + mSoureList.get(position).getJcjhr() + "");
            mHolder.edit = (ImageView) convertView.findViewById(R.id.edit);
            mHolder.editlayout = (LinearLayout) convertView.findViewById(R.id.editlayout);
            mHolder.bjjhr = (TextView) convertView.findViewById(R.id.bjjhr);
            mHolder.bcfxd = (TextView) convertView.findViewById(R.id.bcfxd);


            //给编辑按钮设置click事件并设置拦截事件 返true 事件消费完成，返回false 事件没有消费 事件返回上级控件
            mHolder.edit.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    boolean flag = false;
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            if (mHolder.editlayout.getVisibility() == View.VISIBLE)
                                mHolder.editlayout.setVisibility(View.INVISIBLE);
                            else mHolder.editlayout.setVisibility(View.VISIBLE);
                            flag = true;
                    }
                    return flag;
                }
            });
            //给编辑监护人设置click事件并设置拦截事件 返true 事件消费完成，返回false 事件没有消费 事件返回上级控件
            mHolder.bjjhr.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    boolean flag = false;
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            showEditorGuardianDialog(mSoureList.get(position));
                            flag = true;
                    }
                    return flag;
                }
            });
            //给补充风险点设置click事件并设置拦截事件 返true 事件消费完成，返回false 事件没有消费 事件返回上级控件
            mHolder.bcfxd.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    boolean flag = false;
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            MyToast.showToast("补充风险点");
                            flag = true;
                    }
                    return flag;
                }
            });

            return convertView;
        }

        //显示编辑监护人dialog
       void   showEditorGuardianDialog( CheckPlanBean.RowsBean mRowsBean){
          mEditorGuardianDialog=new EditorGuardianDialog(getContext(),mRowsBean);
          mEditorGuardianDialog.show();
           Window dialogwindow=mEditorGuardianDialog.getWindow();
           WindowManager.LayoutParams lp=dialogwindow.getAttributes();
           lp.width= WindowManager.LayoutParams.MATCH_PARENT;
           lp.height=750;
           dialogwindow.setAttributes(lp);
      }
        //显示补充风险点dialog
        void showAddDangerDailog(){

        }

    }

    static class ViewHolder {
        //施工计划
        TextView jihuahao;
        //施工日期
        TextView shigongriqi;
        //施工类别
        TextView shigongleixing;
        //施工地点
        TextView shigongdidian;
        //施工项目
        TextView shigongxiangmu;
        //施工项目
        TextView jianhurenyuan;

        //编辑监护人按钮
        TextView bjjhr;
        //补充风险点按钮
        TextView bcfxd;

        //控制 editlayout visiable
        ImageView edit;

        //按钮层
        LinearLayout editlayout;

    }


}
